package com.example.parttimejob.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.parttimejob.mapper.Notificationsmapper;
import com.example.parttimejob.pojo.entity.Notifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
public class Notificationscontroller {
    @Autowired
   private Notificationsmapper notificationsmapper;

    //用户使用
    @GetMapping("/notifications/receiver/{receiver_id}")
    public List<Notifications> getNotificationsFromReceiver(@PathVariable int receiver_id) {
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id", receiver_id)
        .in("notification_type", Arrays.asList("用户实名认证成功通知","用户实名认证失败通知","用户账号警告通知","聘用成功通知","聘用失败通知","用户系统通知"));
        return notificationsmapper.selectList(queryWrapper);
    }
    @GetMapping("/notificationsisture/{receiver_id}")
    public Boolean isture(@PathVariable int receiver_id){
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id", receiver_id) .in("notification_type", Arrays.asList("用户实名认证成功通知","用户实名认证失败通知","用户账号警告通知","聘用成功通知","聘用失败通知","用户系统通知"));
        List<Notifications> notificationsList = notificationsmapper.selectList(queryWrapper);
        for (Notifications notification : notificationsList) {
            if(notification.getStatus()==0){
                return false;
            }
        }
        return true;
    }
    @PutMapping("/notificationsisture/{notification_id}")
    public ResponseEntity<Notifications> updatanotification(@PathVariable("notification_id") int notification_id){
        Notifications notifications = notificationsmapper.selectById(notification_id);
        if(notifications != null){
            notifications.setStatus(1);
            notificationsmapper.updateById(notifications); //添加这行代码以执行数据库更新操作
            return ResponseEntity.ok(notifications);
        }
        return ResponseEntity.notFound().build();
    }
    @DeleteMapping("/notifications/{notificationsId}")
    public ResponseEntity<Void> deletenotifications(@PathVariable("notificationsId") Integer notificationsId) {
        int result = notificationsmapper.deleteById(notificationsId);
        return (result > 0) ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }
    @PostMapping("/Notifications")
    public Notifications createNotifications(@RequestBody Notifications user) {
        notificationsmapper.insert(user);
        return user;
    }


    //商家使用

    @GetMapping("/Storenotifications/receiver/{receiver_id}")
    public List<Notifications> StoregetNotificationsFromReceiver(@PathVariable int receiver_id) {
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id", receiver_id)
                .in("notification_type", Arrays.asList("商家实名认证成功通知","商家实名认证失败通知","商家账号警告通知","兼职审核成功通知","兼职审核失败通知","商家系统通知"));

        return notificationsmapper.selectList(queryWrapper);
    }
    @GetMapping("/Storenotificationsisture/{receiver_id}")
    public Boolean Storeisture(@PathVariable int receiver_id){
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id", receiver_id).in("notification_type", "商家实名认证成功通知","商家实名认证失败通知","商家账号警告通知","兼职审核成功通知","兼职审核失败通知","商家系统通知");
        List<Notifications> notificationsList = notificationsmapper.selectList(queryWrapper);
        for (Notifications notification : notificationsList) {
            if(notification.getStatus()==0){
                return false;
            }
        }
        return true;
    }
}
